package com.xianzhiOA.tag;/**
 * Created by admin on 2017/5/25.
 */

import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.bean.XzRole;
import com.xianzhiOA.bean.Xzpower;
import com.xianzhiOA.service.XzpowerService;
import com.xianzhiOA.util.SpringUtil;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.List;

/**
 * 权限标签
 *
 * @author 郭云春晓
 * @create 2017-05-25 14:52
 **/
public class PowerTag extends TagSupport{

    private static final long serialVersionUID =1L;
    private String jspPower;    //页面传递的power
    private String jspRole;     //页面传递的role
    //获取service
    private XzpowerService xzpowerService =
            SpringUtil.getApp().getBean(XzpowerService.class,"xzpowerService");

    @Override
    public int doStartTag() throws JspException {
        HttpSession session = pageContext.getSession();
        //session中获取登录人的角色
        XzEmp emp = (XzEmp) session.getAttribute("userLogin");
        XzRole empRole = emp.getRole();
        if (empRole!=null){
            //获取角色下的权限
            List<Xzpower> powerList = xzpowerService.selectByRoleId(empRole.getRoleId());
            if (jspPower!=null&&!jspPower.equals("")){
                for (Xzpower xzpower:powerList){
                    //判断权限字符是否匹配，若匹配则显示标签内容
                    if (xzpower.getPowerName().equals(jspPower))return EVAL_BODY_INCLUDE;
                }
            }
            if (jspRole!=null&&!jspRole.equals("")){
                if (empRole.getRoleName().equals(jspRole))return EVAL_BODY_INCLUDE;
            }
            //不显示标签内容
            return SKIP_BODY;
        }else {
            return SKIP_BODY;
        }
    }

    public String getJspPower() {
        return jspPower;
    }

    public void setJspPower(String jspPower) {
        this.jspPower = jspPower;
    }

    public String getJspRole() {
        return jspRole;
    }

    public void setJspRole(String jspRole) {
        this.jspRole = jspRole;
    }
}
